package com.fireworks.geoevents;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Actividad que permite configurar determinados aspectos de la aplicacion.
 * 
 * @author Fireworks
 * @version 2/12/2012
 */
public class SettingsActivity extends Activity implements OnClickListener{

	private Ringtone rt;
	private RingtoneManager mRingtoneManager;
	private Button buttonPickSound;
	private Cursor mcursor;
	private Intent Mringtone;
	private String title;


	/*---------------------------------*/
	/* /////////// METODOS /////////// */
	/*---------------------------------*/

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (rt.isPlaying()) {
			rt.stop();
		} else {

		}
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);

		mRingtoneManager = new RingtoneManager(this);
		mcursor = mRingtoneManager.getCursor();
		title = RingtoneManager.EXTRA_RINGTONE_TITLE;

		// Cursor myCursor = myRingtoneManager.getCursor();

	//	text = (TextView) findViewById(R.id.textadd);
		buttonPickSound = (Button) findViewById(R.id.button_sonido);
		buttonPickSound.setOnClickListener(this);

	}

	@Override
	public void onClick(View arg0) {

		Mringtone = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
		Mringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
				RingtoneManager.TYPE_ALARM);

		Mringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
				"Select Tone");

		Mringtone.getBooleanExtra(RingtoneManager.EXTRA_RINGTONE_INCLUDE_DRM,
				true);

		// String uri = null;
		// if (uri != null) {
		// Mringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
		// Uri.parse(uri));
		// } else {
		// Mringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
		// (Uri) null);
		// }

		startActivityForResult(Mringtone, 0);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent Mringtone) {

		switch (resultCode) {
		case RESULT_OK:
			Uri uri = Mringtone
					.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			String test = uri.toString();
			
			buttonPickSound.setText("\n" + test + "\n" + title);

			Log.i("Sample", "uri" + uri);

			play(uri);

			buttonPickSound.append("\n" + mcursor.getCount());

			try {
				RingtoneManager.setActualDefaultRingtoneUri(this, resultCode,
						uri);
			} catch (Exception localException) {

			}
			break;
		}
	}

	private void play(Uri uri) {

		if (uri != null) {
			rt = RingtoneManager.getRingtone(this, uri);
			rt.play();
		}
	}


}

